<template>
	<view class="cart">
		<u-checkbox-group class="cart-group" v-model="checkboxValue" placement="column">
			<view class="cart-item" v-for="(item, index) in cartData" :key="item.id">
				<u-checkbox @change="checkClick(index)" :name="item.id" :checked="item.is_checked===1" shape="circle"></u-checkbox>
				<image :src="item.goods.cover_url"></image>
				<view class="item-right">
					<view class="title">
						<text>{{item.goods.title}}</text>
						<text>{{item.goods.description}}</text>
					</view>
					<view class="item-right-bottom">
						<text class="price">￥{{item.goods.price}}</text>
						<view class="adjust">
							<text class="decrease" @click="decrease(index, item.id)">-</text>
							<text class="number">{{item.num}}</text>
							<text class="increase" @click="increase(index, item.id)">+</text>
						</view>
						<u-icon name="trash" color="red" size="40rpx" @click="deleteCart(item.id)"></u-icon>
					</view>
				</view>
			</view>
			<u-modal :show="isDelete" title="确认删除该商品？" showCancelButton @confirm="confirm" @cancel="cancel"></u-modal>
		</u-checkbox-group>
		<view class="account">
			<u-checkbox-group>
				<u-checkbox class="all-select" shape="circle" label="全选" :checked="allCheck" @change="allCheckClick"></u-checkbox>
			</u-checkbox-group>
			<view class="all-price">合计：<text>￥{{totalPrice}}</text></view>
			<u-button class="calc" type="error" shape="circle" text="去结算 " size="normal" @click="toCalc"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checkboxValue:[],
				cartData: [],
				allCheck: false,
				cartIds: [],
				totalPrice: 0,
				isDelete: false,
				deleteId: 0
			}
		},
		onLoad() {
			this.getCartList()
		},
		onShow() {
			this.getCartList()
		},
		methods: {
			// 每个多选框点击事件
			checkClick(index) {
				if (this.cartData[index].is_checked === 0) {
					this.cartData[index].is_checked = 1
					if (!this.cartIds.includes(this.cartData[index].id)) {
						this.cartIds.push(this.cartData[index].id)
					}
				}else {
					this.cartData[index].is_checked = 0
					const flag = this.cartIds.indexOf(this.cartData[index].id)
					if (flag > -1) {
						this.cartIds.splice(flag, 1)
					}
				}
				this.changeCart(this.cartIds)
				// 判断是否全选
				this.allCheck = true
				this.cartData.forEach((item) => {
					if (item.is_checked == 0) {
						this.allCheck = false
					}
				})
				
				// 计算总价格
				this.sunPrice()
			},
			// 全选按键点击事件
			allCheckClick(boolean) {
				this.allCheck = boolean
				this.cartData.forEach((item) => {
					if (boolean) {
						item.is_checked = 1
						this.cartIds = this.cartData.map(item => {
							return item.id
						})
					}else {
						item.is_checked = 0
						this.cartIds = []
					}
				})
				// 发送改变选中请求
				this.changeCart(this.cartIds)
				
				// 计算总价格
				this.sunPrice()
			},
			// 请求购物车数据列表
			async getCartList() {
				const res = await uni.$u.http.get('/api/carts', {
					params: {
						include: 'goods'
					}
				})
				this.cartData = res.data
				console.log(this.cartData)
				this.allCheck = true
				this.cartIds = this.cartData.map(item => {
					if (item.is_checked === 1) {
						return item.id
					}else {
						// 判断是否默认全部选中
						this.allCheck = false
					}
				})
				// 计算总价格
				this.sunPrice()
			},
			decrease(index, id) {
				if (this.cartData[index].num > 1) {
					this.cartData[index].num--
					this.requestNum(id, this.cartData[index].num)
					// 计算总价格
					if (this.cartData[index].is_checked === 1) {
						this.sunPrice()
					}
				}
			},
			increase(index, id) {
				this.cartData[index].num++
				this.requestNum(id, this.cartData[index].num)
				// 计算总价格
				if (this.cartData[index].is_checked === 1) {
					this.sunPrice()
				}
			},
			// 请求修改购物车数量
			async requestNum(id, num) {
				await uni.$u.http.put('/api/carts/'+id, {
					num
				})
			},
			// 移出购物车
			deleteCart(id) {
				// await uni.$u.http.delete('/api/carts/'+id)
				// this.getCartList()
				this.isDelete=true
				this.deleteId = id
			},
			// 购物车改变选中
			async changeCart(cartIds) {
				await uni.$u.http.patch('/api/carts/checked', {
					cart_ids: cartIds
				})
			},
			// 合计总价
			sunPrice() {
				let total = this.cartData.reduce((pre, next)=> {
					if (next.is_checked === 1) {
						return pre + next.num * next.goods.price
					}else {
						return pre
					}
				}, 0)
				this.totalPrice = total.toFixed(2)
			},
			// 跳转到结算页面
			toCalc() {
				// 判断是否有选中一个物品
				const isPick = this.cartData.some(item => {
					return item.is_checked == 1
				})
				if (!isPick) {
					uni.$u.toast('未选中商品')
					return
				}
				
				uni.$u.route({
					url: 'pages/cart/order',
				})
			},
			// 取消删除
			cancel() {
				this.isDelete = false
			},
			// 确定删除
			async confirm() {
				await uni.$u.http.delete('/api/carts/'+this.deleteId)
				this.isDelete = false
				this.getCartList()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cart {
		.cart-group {
			margin-bottom: 100rpx;
		}
		.cart-item {
			display: flex;
			padding: 20rpx 40rpx;
			border-bottom: 2px solid #ccc;
			&:nth-last-child(1) {
				border-bottom: none;
			}
			image {
				width: 200rpx;
				height: 160rpx;
			}
			.item-right {
				flex: 1;
				.title {
					display: flex;
					flex-direction: column;
					margin-bottom: 40rpx;
					text {
						font-size: 28rpx;
						&:nth-child(1) {
							font-weight: 700;
						}
						&:nth-child(2) {
							color: #666;
						}
					}
					
				}
				.item-right-bottom {
					display: flex;
					justify-content: space-between;
					.price {
						color: red;
						font-weight: 700;
						width: 100rpx;
					}
					.adjust {
						text-align: center;
						text {
							display: inline-block;
							width: 50rpx;
							height: 50rpx;
							background-color: #eee;
						}
						.number {
							margin: 0 20rpx;
						}
					}
				}
			}
		}
		.account {
			position: fixed;
			bottom: 100rpx;
			width: 100%;
			display: flex;
			justify-content: space-around;
			background-color: #FFFFFF;
			.all-select {
				margin-left: 40rpx;
				flex: 1;
			}
			.all-price {
				flex: 2;
				text-align: center;
				line-height: 100rpx;
				text {
					color: red;
				}
			}
			.calc {
				flex: 1;
				margin: 10rpx 40rpx 10rpx 0;
			}
		}
	}
	

</style>
